extends Node

var http_request: HTTPRequest
var url: String = "https://github.com/shikeik/project-template/releases/download/multiplayergamedemo/official-server.json"
var json: JSON  # 声明 JSON 类实例

func _ready():
	# 创建并添加 HTTPRequest 实例
	http_request = HTTPRequest.new()
	add_child(http_request)
	
	# 创建 JSON 实例
	json = JSON.new()
	
	# 连接请求完成信号
	http_request.request_completed.connect(_on_request_completed)
	
	# 设置请求头（添加 User-Agent）
	var headers = ["User-Agent: Godot Engine"]
	
	# 发起 HTTP 请求，附加请求头
	var error = http_request.request(url, headers)
	if error != OK:
		print("请求失败: ", error)

func _on_request_completed(result, response_code, headers, body):
	if response_code == 200:
		# 解析 JSON 数据
		var json_result = json.parse(body.get_string_from_utf8())
		
		if json_result == OK:
			var json_data = json.get_data()  # 获取解析后的数据，返回 Dictionary 或 Array
			print("解析成功:", json_data)
		else:
			print("JSON 解析失败")
	else:
		print("请求失败，状态码: ", response_code)
